import string
import random
import os
from PIL import Image, ImageDraw, ImageFont,ImageFilter
class   ImageVerifyUtil:
    def __init__(self,width = 140,height= 40,length = 4,size = 28):
        """

        :param width:  宽
        :param height: 高
        :param length: 验证码个数
        :param size: 字体大小
        """
        self.width = width
        self.height = height
        self.length = length
        self.size = size
    # 验证码

    def random_str(self):
        """
        :return: 字符串
        """
        "a-zA-Z0-9"
        sourse = string.ascii_letters+string.digits
        # res = random.choices(sourse,k = self.length)  #["",]
        # res = "".join(random.choices(sourse,k = self.length))
        return "".join(random.choices(sourse,k = self.length))

    # 颜色 rgb(0,0,0)  rgb(255,255,255)
    def random_color(self,start = 0,end = 255):
        """
        :param start: 三原色的起始值
        :param end: 三原色的结束值
        :return:  颜色 (1,200,156)   元组
        """
        # li = []
        # for i in range(3):
        #     li.append(random.randint(start,end))
        li = [random.randint(start,end) for i in range(3)]  #[1,200,156]
        return tuple(li)

    # 干扰线
    def random_lines(self,draw):
        """
        :param draw: 画笔
        """
        # li = []
        # # 起始点  结束点
        # for i in range(2):
        #     # random.randint(0,self.width)  # x点
        #     # random.randint(0,self.height) #y 点
        #    li.append( (random.randint(0,self.width), random.randint(0,self.height))) #[(x,y),(x,y)]

        # 干扰线的条数
        for i in range(self.length):
            # # # 起始点  结束点 [(x,y),(x,y)]
            lines = [(random.randint(0,self.width), random.randint(0,self.height)) for i in range(2)]
            # 绘制成线条
            # line(坐标,线条颜色,线条粗细)
            draw.line(lines,fill=self.random_color(0,255),width=2)

    # 干扰点
    def random_ponints(self,draw):
        """
        :param draw: 画笔
        """
        for x in range(self.width): # 获取行
            for y in  range(self.height): # 获取列
                # 干扰点的比例
                # random.randint(100) <= 10;  10%
                if random.randint(0,100) <=10:
                    draw.point((x,y),fill=self.random_color(0,255))


    # 整合验证码图片
    def verify_code(self):
        """

        :return:  img 图片返回   code 验证码内容
        # 画布
        # 画笔
        """
        # 生成画布
        image = Image.new('RGB',(self.width,self.height),(255,255,255))
        # 创建字体对象
        file = os.path.dirname(os.path.abspath(__file__)) #"utils/"
        # 指定字体路径
        font = ImageFont.truetype(f'{file}/HYYakuHei-85W.ttf', self.size)
        # 画 通过画布对象 生成画布对象的画笔
        draw = ImageDraw.Draw(image)
        # 返回
        self.random_lines(draw)
        self.random_ponints(draw)
        # 得到验证码
        code = self.random_str() #"abcd"
        # 吧验证码写入到画布当中
        #(0) (35*1) (35*2)  (35*3)
        for i in range(self.length): #i  = 0 1 2 3
            draw.text((i*35,random.randint(3,6)),code[i],font=font,fill=self.random_color(0,255))
        # 高斯模糊
        # image= image.filter(ImageFilter.BLUR)
        # 图片  信息
        return image,code

if __name__ == '__main__':
    imageVerifyUtil = ImageVerifyUtil()
    img,code = imageVerifyUtil.verify_code()
    with open("test.png","wb") as f:
        img.save(f)

    print(code)
